package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.ExclusiveServiceAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.dto.ServiceQQ;
import com.hmyd.hmyd_android.utils.ExclusiveServicePop;
import com.hmyd.hmyd_android.utils.PhotoTest;
import com.hmyd.hmyd_android.utils.ExclusiveServicePop.onGetQQListener;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_exclusive_service)
public class ExclusiveServiceActivity extends BasicActivity implements onGetQQListener {
	private GridView mGridView;
	private List<ServiceQQ> datas = new ArrayList<ServiceQQ>();
	private ExclusiveServiceAdapter mAdapter;
	private View homeView;
	private ExclusiveServicePop exclusiveServicePop;

	private final int QQLIST = 1;

	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mGridView = (GridView) findViewById(R.id.exclusive_service);
		homeView = findViewById(R.id.parent);
		
		onRequest(QQLIST, UrlConstant.SERVICE_QQ, HttpMethod.GET, null);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
	}

	@Override
	protected void setParams() {
		home_title_txt.setText("专属客服");
		home_right.setText("滑动显示");
		home_right.setVisibility(View.VISIBLE);

		home_title.setOnTouchListener(touch);

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}


	OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			exclusiveServicePop = new ExclusiveServicePop(ExclusiveServiceActivity.this,
					datas.get(position));
			exclusiveServicePop.showAtLocation(homeView, Gravity.CENTER, 0, 0);

			Drawable drawable = getResources().getDrawable(R.drawable.geng_icon_kfimg);
			int width = drawable.getIntrinsicWidth();
			int height = drawable.getIntrinsicHeight();
			Bitmap bitmap = Bitmap.createBitmap(width, height,
					drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
							: Bitmap.Config.RGB_565);
			Canvas canvas = new Canvas(bitmap);
			drawable.setBounds(0, 0, width, height);
			drawable.draw(canvas);

			Drawable drawable1 = new BitmapDrawable(getResources(), PhotoTest.fastblur(
					ExclusiveServiceActivity.this, bitmap, 30));

			for (int i = 0; i < mGridView.getChildCount(); i++) {
				ImageView imageView = (ImageView) mGridView.getChildAt(i).findViewById(R.id.iv);
				imageView.setImageDrawable(drawable1);
			}

		}
	};

	@Override
	public Void onGet(String qq) {
		Intent intent = new Intent();
		intent.putExtra("qq", qq);
		setResult(RESULT_OK, intent);
		finish();
		return null;
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<ServiceQQ>>() {
		}.getType();
		Page<ServiceQQ> page = gson.fromJson(result, type2);
		if (page != null && page.records != null) {
			datas.addAll(page.records);
			mAdapter = new ExclusiveServiceAdapter(ExclusiveServiceActivity.this, datas);
			mGridView.setAdapter(mAdapter);
			mGridView.setOnItemClickListener(listener);
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}

}
